<!DOCTYPE html>
<html lang="zxx">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="robots" content="noindex, follow"/>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/plugins.min.css">
    <link rel="stylesheet" href="css/vendor.min.css">
    <link rel="stylesheet" href="css/style.min.css">
    <script src="js/jquery/jquery.min.js"></script>
    <style>
        .section-space-top-100{
            padding-top: 30px;
        }
        html{
            height: 100%;
            overflow-y: hidden;
        }
        body{
            height: 100%;
            overflow-y: auto;
        }
        .num{
            border: 1px solid black;height: 30px;width: 40px;border-radius: 4px;text-align: center;
        }
        .guige_btn{
            margin-top:10px;
            height:25px;
            padding:0 10px;
            line-height:25px;
            display:flex;
            background:#e8e8e8;
            border-radius:3px;
            margin-left:10px;
            align-items:center;
            cursor: pointer;
        }
        .guige_selected{
            border:1px solid #ff5555;
            color:#ff5555;
            background:#f1e9e9;
        }
    </style>
</head>

<body>
<div class="main-wrapper">
    <main class="main-content">
        <div class="single-product-area section-space-top-100">
            <div class="container">
                <div class="row">
                    <div class="col-lg-6">
                        <div class="single-product-img">
                            <div class="single-img img-zoom-effect">
                                <img class="img-full" style="border-radius: 20px;">
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-6 pt-9 pt-lg-0">
                        <div class="single-product-content with-sticky">
                            <h2 class="title">腾讯大王卡2.0（网厅）</h2>
                            <div class="price-box pb-1">
                                <span class="new-price text-danger" id="price" style="font-size: 20px;">￥80.00</span>
                            </div>
                            <div id="detail-info" style="">

                            </div>
                            <div id="guiges">

                            </div>
                            <p class="mb-6" id="Total-price"></p>
                            <ul class="quantity-with-btn pb-7" style="display: inline;">
                                <li class="quantity">
                                    <button class="num" style="padding: 0px 10px; size: 30px;" onclick="goNum(1)">-</button>
                                    <input type="text" class="num" value="1" id="num">
                                    <button class="num" style="padding: 0px 10px; size: 30px" onclick="goNum(2)">+</button>
                                </li>
                                <li class="add-to-cart" style="margin-top: 20px;">
                                    <a class="btn btn-custom-size lg-size btn-primary btn-secondary-hover rounded-0"
                                       id="add" onclick="puanduan()" >加入购物车</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="product-tab-area section-space-top-100">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <ul class="nav product-tab-nav product-tab-style-2" role="tablist">
                            <li class="nav-item" role="presentation">
                                <a class="active btn btn-custom-size" id="description-tab" data-bs-toggle="tab"
                                   href="#description" role="tab" aria-controls="description" aria-selected="true">
                                    商品详情
                                </a>
                            </li>
                        </ul>
                        <div class="tab-content product-tab-content" style="padding-bottom: 50px;">
                            <div class="tab-pane fade show active" id="description" role="tabpanel"
                                 aria-labelledby="description-tab">
                                <div class="product-description-body">
                                    <div class="short-desc mb-0">商品详情</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
<!--        <div class="product-slider-area section-space-top-95 section-space-bottom-100">-->
<!--            <div class="container">-->
<!--                <div class="section-title text-center pb-55">-->
<!--                    <h2 class="title mb-0">商品推荐</h2>-->
<!--                </div>-->
<!--                <div class="row">-->
<!--                    <div class="col-lg-12">-->
<!--                        <div class="swiper-slider-holder swiper-arrow">-->
<!--                            <div class="swiper-container product-slider border-issue">-->
<!--                                <div class="swiper-wrapper">-->
<!--                                    <div class="swiper-slide">-->
<!--                                        <div class="product-item" style="width: 260px;">-->
<!--                                            <div class="product-img img-zoom-effect">-->
<!--                                                <a href="single-product.html">-->
<!--                                                    <img class="img-full" src="img/no.jpg" style="width: 260px;height: 260px;">-->
<!--                                                </a>-->
<!--                                                <div class="product-add-action">-->
<!--                                                    <ul>-->
<!--                                                        <li>-->
<!--                                                            <a href="cart.html">-->
<!--                                                                <i class="pe-7s-cart"></i>-->
<!--                                                            </a>-->
<!--                                                        </li>-->
<!--                                                        <li>-->
<!--                                                            <a href="compare.html">-->
<!--                                                                <i class="pe-7s-shuffle"></i>-->
<!--                                                            </a>-->
<!--                                                        </li>-->
<!--                                                        <li>-->
<!--                                                            <a href="wishlist.html">-->
<!--                                                                <i class="pe-7s-like"></i>-->
<!--                                                            </a>-->
<!--                                                        </li>-->
<!--                                                    </ul>-->
<!--                                                </div>-->
<!--                                            </div>-->
<!--                                            <div class="product-content texx">-->
<!--                                                <a class="product-name" href="single-product.html">腾讯5G卡</a>-->
<!--                                                <div class="price-box pb-1">-->
<!--                                                    <span class="new-price">￥80.00</span>-->
<!--                                                </div>-->
<!--                                                <div class="rating-box">-->
<!--                                                    <ul>-->
<!--                                                        <li><i class="pe-7s-star"></i></li>-->
<!--                                                        <li><i class="pe-7s-star"></i></li>-->
<!--                                                        <li><i class="pe-7s-star"></i></li>-->
<!--                                                        <li><i class="pe-7s-star"></i></li>-->
<!--                                                        <li><i class="pe-7s-star"></i></li>-->
<!--                                                    </ul>-->
<!--                                                </div>-->
<!--                                            </div>-->
<!--                                        </div>-->
<!--                                    </div>-->
<!--                                </div>-->
<!--                            </div>-->
<!--                        </div>-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
<!--        </div>-->
    </main>
    <div class="footer-area">
        <div class="footer-bottom py-3" data-bg-color="#1EB9EE" style="background-color: rgb(30, 185, 238);">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="copyright">
                            <span class="copyright-text text-white">Copyright © 2023.LAO Asia Unicom All rights reserved.</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <a class="scroll-to-top" href="">
        <i class="fa fa-chevron-up"></i>
    </a>
</div>
<script src="layui/layui.js"></script>
<script src="js/config.js"></script>
<script src="js/token.js"></script>
<script src="js/vendor.min.js"></script>
<script src="js/plugins.min.js"></script>
<script src="js/jquery.i18n.min.js"></script>
<script src="js/multi_lang_config.js"></script>

</body>
<script>

    var layer = layui.layer;
    var form = layui.form;

    var moduleName = "shop";
    var defaultLang = layui.data('langTab').langType;
    initLangConfig({
        defaultLang:defaultLang,
        filePath: "js/i18n/"+moduleName+"/",
        module:moduleName,
        base:"js/"
    })
    var i18np = null;
    function changeLang(lang) {
        defaultLang = lang;
        reloadI18n({
            defaultLang:lang,
            filePath: "js/i18n/"+moduleName+"/",
            module:moduleName,
        })
        i18np.loadProperties(moduleName);
    }

    layui.config({base: 'js/'})
        // 继承treetable.js插件
        .extend({i18np: 'i18n'}).use([ 'i18np','jquery'], function () {
        i18np = layui.i18np;
        i18np.loadProperties(moduleName);
    });

    //商品数量加减
    function goNum(type) {
        var num = $("#num").val();
        if (type == 1){
            if (num > 1){
                num--;
            }else {
                num = 1;
            }
        }else if (type == 2){
            num++;
        }
        $("#num").val(num);
    }

    // 添加到购物车
    function puanduan() {
        if(!check()) {
            layer.msg("请将规格选完整！",{icon:2});
            return false;
        }
        if (kucun != "有库存" && kucun <= 0){
            layer.msg("库存不足！",{icon:2});
            return false;
        }
        $.ajax({
            url:HOST+"webapi/cart/addgood",
            headers:{"token":localStorage.getItem("token")},
            type:"get",
            data:{
                "gid":gid,
                "num":$("#num").val(),
                "names":guige==null ? "" : JSON.stringify(names).replace(/\[/g , "").replace(/\]/g , "").replace(/"/g , ""),
                "params":guige==null ? "" : encodeURI(params).replace(/\[/g , "").replace(/\]/g , "").replace(/"/g , ""),
                "money":gprice,
                "username":localStorage.getItem("laoName"),
                "userId":localStorage.getItem("userId")
            },
            success:function (res) {
                if(res.code==200){
                    layer.msg("加入购物车成功！",{icon:1});
                    localStorage.setItem("userId",res.data);
                    setTimeout(function(){
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);//关闭弹出层
                        parent.location.reload();//重新加载父页面
                    },1500);
                }
            }
        });
    }

    var gid = GetQueryString("gid");
    var params = [];
    var names = [];
    var price = null;//价格
    var kucun = "有库存";
    var guige = null;//规格
    var gprice = null;//规格价格
    var goodsData = {};//商品信息
    function initGoodsData() {
        $("#detail-info").html(goodsData.jianjie);
        if (defaultLang == "zh"){
            $(".single-product-content .title").html(goodsData.nameZh);
            $(".short-desc").html(goodsData.detailZh);
        }else if (defaultLang == "en"){
            $(".single-product-content .title").html(goodsData.nameEn);
            $(".short-desc").html(goodsData.detailEn);
        }else if (defaultLang == "la"){
            $(".single-product-content .title").html(goodsData.nameLao);
            $(".short-desc").html(goodsData.detailLao);
        }
        if(goodsData.guigeZh != null && goodsData.guigeZh.trim() != "") {
            guige = JSON.parse(goodsData.guigeZh)
            if(guige.values.length == 0) {
                guige = null
            }else{
                if(guige.select_guiges.length == 1) {
                    for(var m in guige.values) {
                        if(guige.values[m].kucun <= 0) {
                            $(".guige0:eq("+m+")").css("display" , "none")
                        }
                    }
                }
            }
            $("#guiges div").remove();
            for(var i in guige.select_guiges) {
                $("#guiges").append($("<div style=\"margin:10px;\">\n" +
                    "\t\t\t <div style=\"background-color:#e8e7e7;height:1px;\"></div>\n" +
                    "\t\t\t </div>"))
                var ele = $("<div style=\"margin:10px;color:#505050;font-size:14px;\"></div>")
                ele.append("<div>"+guige.select_guiges[i].name+"</div>")

                var g = $("<div style=\"display:flex;flex-wrap: wrap;margin-left:-10px;\"></div>")
                if(i == 0) {
                    for(var j in guige.values) {
                        g.append("<div name='{\""+guige.select_guiges[0].name + "\":\"" +guige.values[j].name+"\"}' index='"+j+"' class=\"guige_btn guige0\"><img style=\"height:23px;width:23px;\" src=\""+guige.values[j].img+"\" /><span style=\"margin-left:4px;font-size:12px;\">"+guige.values[j].name+"</span></div>")
                    }
                }
                if(i == 1) {
                    for(var j in guige.values[0].children) {
                        g.append("<div name='{\""+guige.select_guiges[1].name + "\":\"" +guige.values[0].children[j].name+"\"}' index='"+j+"' class=\"guige_btn guige1\"><span style=\"font-size:12px;\">"+guige.values[0].children[j].name+"</span></div>")
                    }
                }
                if(i == 2) {
                    for(var j in guige.values[0].children[0].children) {
                        g.append("<div name='{\""+guige.select_guiges[2].name + "\":\"" +guige.values[0].children[0].children[j].name+"\"}' index='"+j+"' class=\"guige_btn guige2\"><span style=\"font-size:12px;\">"+guige.values[0].children[0].children[j].name+"</span></div>")
                    }
                }
                ele.append(g)
                $("#guiges").append(ele)
            }

            $(".guige0").click(function(){
                $(".guige0").each(function(){
                    $(this).removeClass("guige_selected")
                })
                $(this).addClass("guige_selected")
                params[0] = $(this).attr("index")
                names[0] = JSON.parse($(this).attr("name"))
                check(1)
            })

            $(".guige1").click(function(){
                $(".guige1").each(function(){
                    $(this).removeClass("guige_selected")
                })
                $(this).addClass("guige_selected")
                params[1] = $(this).attr("index")
                names[1] = JSON.parse($(this).attr("name"))
                check(2)
            })

            $(".guige2").click(function(){
                $(".guige2").each(function(){
                    $(this).removeClass("guige_selected")
                })
                $(this).addClass("guige_selected")
                params[2] = $(this).attr("index")
                names[2] = JSON.parse($(this).attr("name"))
                check(3)
            })
        }else{
            $("#guiges").hide();
        }
    }

    //验证规格
    function check(idx){
        if(idx == 1 && guige.select_guiges.length == 2) {
            $(".guige1").css("display" , "block")
            for(var i in guige.values[params[0]].children) {
                if(guige.values[params[0]].children[i].kucun <= 0) {
                    $(".guige1:eq("+i+")").css("display" , "none")
                }
            }
            // $("#cover").attr("src" , guige.values[params[0]].img);
        }
        if(idx == 2 && guige.select_guiges.length == 3) {
            $(".guige2").css("display" , "block")
            for(var i in guige.values[params[0]].children[params[1]].children) {
                if(guige.values[params[0]].children[params[1]].children[i].kucun <= 0) {
                    $(".guige2:eq("+i+")").css("display" , "none")
                }
            }
        }
        if(params.length == guige.select_guiges.length) {
            if(params.length == 1) {
                gprice = guige.values[params[0]].jiage
                kucun = guige.values[params[0]].kucun;
            }
            if(params.length == 2) {
                gprice = guige.values[params[0]].children[params[1]].jiage
                kucun = guige.values[params[0]].children[params[1]].kucun;
            }
            if(params.length == 3) {
                gprice = guige.values[params[0]].children[params[1]].children[params[2]].jiage
                kucun = guige.values[params[0]].children[params[1]].children[params[2]].kucun;
            }
            $("#price").html(gprice+"₭");
            // console.log("规格价格："+gprice+" 规格库存："+kucun);
            // console.log(params);
            // console.log(names);
            return true
        }
        return false
    }

    // 查询商品详情
    $.ajax({
        url: HOST + "webapi/goods/getGoodsById",
        data:{gid:gid},
        headers:{lang:"en"},
        success: function (res) {
            if(res.code == 200){
                $(".single-product-area .img-full").attr("src",res.data.picture);
                price = res.data.price;
                gprice = price;
                $("#price").html(price+"₭");
                goodsData = res.data;
                initGoodsData();
            }
        }
    })
</script>

</html>